package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysBaseCountry;
import com.maoye.ossp.sys.base.service.SysBaseCountryService;
@Controller
@RequestMapping(value = "sys/base/country/")
public class SysBaseCountryController extends BaseController {
	@Resource
	private SysBaseCountryService sysBaseCountryService;
	
	/**
	 * 获得国家列表
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "getCountryList")
	public String getCountryList(HttpServletRequest request, Model model,
			SysBaseCountry country) {
		QueryFilter queryFilter = getQuerFilter(request,country);
		Page<SysBaseCountry> page = (Page<SysBaseCountry>)sysBaseCountryService
				.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/sys/base/country/getCountryList", model, request);
	}
	@RequestMapping(value = "addCountry")
	public String addCountry(HttpServletRequest request, Model model)
			throws IOException {
		Map map = new HashMap();
		SysBaseCountry sysBaseCountry = new SysBaseCountry();
		model.addAttribute("sysBaseCountry",sysBaseCountry);
		return "/sys/base/country/add";
	}
	
	/**
	 * 简称国家编码
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="checkCountryCode")
	public void checkCountryCode(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Map map = new HashMap();
		String code = request.getParameter("code");	
		map.put("code",code);
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		if(sysBaseCountryService.checkCountryCode(map)){
			json.put("resCode",0); //区域号可用
		}else{
			json.put("resCode",1);//区域号不可以用
		}
		out.print(json.toString());
		out.flush();
		out.close();
	}
	
	/**
	 * 保存国家信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "saveCountry")
	public String saveCountry(SysBaseCountry country,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException{
		country.setId(idGenerator.getUId());
		country.setCode(country.getCode().replaceAll(",","").toString());
		//设置创建人
		country.setCreateBy("");
		//设置修改人
		country.setUpdateBy("");
		sysBaseCountryService.saveCountry(country);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:getCountryList";
	}	
	/**
	 * 查看国家信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="viewCountry")
	public String viewCountry(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseCountry country=null;
		country=sysBaseCountryService.getCountrybyCode(map);
		model.addAttribute("country",country);
		return "/sys/base/country/view";
	}
	@RequestMapping(value ="updateCountry")
	public String updateCountry(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseCountry sysBaseCountry=null;
		sysBaseCountry=sysBaseCountryService.getCountrybyCode(map);
		model.addAttribute("sysBaseCountry",sysBaseCountry);
		return "/sys/base/country/update";
	}
	/**
	 * 修改国家信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="saveUpdateCountry")
	public String saveUpdateCountry(HttpServletRequest request,HttpServletResponse response,SysBaseCountry country,RedirectAttributes redirectAttributes){
		//设置创建人
		country.setCreateBy("");
		//设置修改人
		country.setUpdateBy("");
		sysBaseCountryService.saveUpdateCountry(country);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:getCountryList";
	}
	/**
	 * 删除国家信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="removeCountry")
	public String removeCountry(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		Map map = new HashMap();
		String code=request.getParameter("code");
		map.put("code",code);
		sysBaseCountryService.removeCountry(map);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:getCountryList";
	}
}
